package com.device.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.device.Constant.BaseConstant;
import com.device.Entity.Device;
import com.device.Entity.Lab;
import com.device.Service.DeviceService;
import com.device.Service.LabService;
import com.device.annotation.GlobalInterceptor;
import com.device.common.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Resource
    private LabService labService;
    @Resource
    private DeviceService deviceService;

    @RequestMapping("/count")
    @GlobalInterceptor(checkLogin = true)
    public Result count() {
        Map<String, Object> map = new HashMap<>();
        Long all = labService.count();
        Long free = selectcount(BaseConstant.ONE);
        Long using = selectcount(BaseConstant.ZERO);
        Long repaire = selectcount(BaseConstant.TWO);

        map.put("sysall", all);
        map.put("free", free);
        map.put("using", using);
        map.put("repaire", repaire);

        return Result.success(map);
    }

    @RequestMapping("/chart1")
    @GlobalInterceptor(checkLogin = true)
    public Result chart1() {
        List<Map<String,Object>> list=new ArrayList<>();
        List<Device> deviceList = deviceService.list();
        Map<String,Object> map=new HashMap<>();
        map.put("name","空闲中");
        map.put("value",selectcount(BaseConstant.ONE));
        list.add(map);
        Map<String,Object> map1=new HashMap<>();
        map1.put("name","使用中");
        map1.put("value",selectcount(BaseConstant.ZERO));
        list.add(map1);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name","维修中");
        map2.put("value",selectcount(BaseConstant.TWO));
        list.add(map2);
        return Result.success(list);


    }

    @RequestMapping("/chart")
    @GlobalInterceptor(checkLogin = true)
    public Result chart() {
        List<Lab> labList = labService.list();
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        List<Long> list1 = new ArrayList<>();
        List<Long> list2 = new ArrayList<>();
        List<Long> list3 = new ArrayList<>();
        for (Lab lab : labList) {
            list.add(lab.getName());
            list1.add(selectstatuscount(lab, BaseConstant.ZERO));
            list2.add(selectstatuscount(lab, BaseConstant.ONE));
            list3.add(selectstatuscount(lab, BaseConstant.TWO));
        }
        map.put("x", list);
        map.put("using", list1);
        map.put("free", list2);
        map.put("repaire", list3);
        return Result.success(map);
    }

    public Long selectstatuscount(Lab lab, Integer status) {
        LambdaQueryWrapper<Device> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Device::getStatus, status);
        lambdaQueryWrapper.eq(Device::getSysid, lab.getId());
        Long count = deviceService.count(lambdaQueryWrapper);
        return count;
    }

    public Long selectcount(Integer status) {
        LambdaQueryWrapper<Device> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Device::getStatus, status);
        Long count = deviceService.count(lambdaQueryWrapper);
        return count;
    }
}
